<template>
  <div class="code-layout">
    <div class="code-materials">
      <plug-state :statusStr="status" />
      <br />
      <div>
        <dl class="code-dl" v-for="(e, i) in version.tcp_port_list" :key="i">
          <li v-if="e.endpoint">
            <span class="key">端口{{ e.containerPort }}:</span>
            <span class="val"> {{ e.endpoint }}</span>
          </li>
        </dl>
      </div>
      <div class="file">
        <s3-file-list :s3_access_id="version.s3_access_id" :s3_path="version.s3_path" v-if="version.s3_access_id" />
        <file-list :biz_id="version.id" type="workspace" v-else />
      </div>
      <md-preview :biz_id="$route.query.id" type="workspace" class="preview" />
    </div>
    <div class="code-info">
      <h3 class="info-title">基本信息</h3>
      <ul class="info-item">
        <li>
          <span class="key">名称:</span>
          <span class="val">{{ detailData.name }}</span>
        </li>
        <li>
          <span class="key"> 创建人： </span>
          <span class="val">{{
            detailData.create_user && (detailData.create_user.nick_name ||
              detailData.create_user.username)
          }}</span>
        </li>
        <li>
          <span class="key">创建时间： </span>
          <span class="val">{{ detailData.create_time }}</span>
        </li>
      </ul>

      <h3 class="info-title">开发环境<i class="el-icon-edit-outline edit" @click="updateEnv" /></h3>
      <dl class="code-dl" v-for="(e, i) in version.node_group_list" :key="i">
        <ul class="info-item">
          <li>
            <span class="key">节点类型:</span>
            <span class="val">{{ nodeTypeMaps[e.type] }}</span>
          </li>
          <li>
            <span class="key">资源组:</span>
            <span class="val">{{ e.product.name }}</span>
          </li>
          <li>
            <span class="key">镜像:</span>
            <span class="val">{{ e.image.name }}</span>
          </li>
          <li>
            <span class="key">数量:</span>
            <span class="val">{{ e.pod_num }}</span>
          </li>
          <li>
            <span class="key">启动命令行:</span>
            <span class="val">{{ e.command }}</span>
          </li>
        </ul>
      </dl>
      <h3 class="info-title">数据集<i class="el-icon-edit-outline edit" @click="updateDataset" /></h3>
      <ul class="info-item">
        <dl class="code-dl" v-for="(e, i) in version.dataset_list" :key="i">
          <li>
            <span class="key">名称:</span>
            <span class="val">{{ e.dataset.name }}</span>
          </li>
          <li>
            <span class="key">挂载地址:</span>
            <span class="val">/dataset/{{ e.dataset.alias }}</span>
          </li>
        </dl>
      </ul>
      <h3 class="info-title">预训练模型<i class="el-icon-edit-outline edit" @click="updateModel" /></h3>
      <ul class="info-item">
        <dl class="code-dl" v-for="(e, i) in version.model_list" :key="i">
          <li>
            <span class="key">名称:</span>
            <span class="val">{{ e.model.name }}</span>
          </li>
          <li>
            <span class="key">挂载地址:</span>
            <span class="val">/model/{{ e.model.alias }}</span>
          </li>
        </dl>
      </ul>

      <h3 class="info-title" v-if="s3_flag">S3输入<i class="el-icon-edit-outline edit" @click="updateInS3" /></h3>
      <ul class="info-item" v-if="s3_flag">
        <dl class="code-dl" v-for="(e, i) in in_s3_list" :key="i">
          <li>
            <span class="key">名称:</span>
            <span class="val">{{ e.s3_name }}</span>
          </li>
          <li>
            <span class="key">桶:</span>
            <span class="val">{{ s3idFormat(e.s3_id) }}</span>
          </li>
          <li>
            <span class="key">路径:</span>
            <span class="val">{{ e.path }}</span>
          </li>
          <li>
            <span class="key">挂载地址:</span>
            <span class="val">/input/{{ e.s3_name }}</span>
          </li>
        </dl>
      </ul>

      <h3 class="info-title" v-if="s3_flag">S3输出<i class="el-icon-edit-outline edit" @click="updateOutS3" /></h3>
      <ul class="info-item" v-if="s3_flag">
        <dl class="code-dl" v-for="(e, i) in out_s3_list" :key="i">
          <li>
            <span class="key">名称:</span>
            <span class="val">{{ e.s3_name }}</span>
          </li>
          <li>
            <span class="key">桶:</span>
            <span class="val">{{ s3idFormat(e.s3_id) }}</span>
          </li>
          <li>
            <span class="key">路径:</span>
            <span class="val">{{ e.path }}</span>
          </li>
          <li>
            <span class="key">挂载地址:</span>
            <span class="val">/output/{{ e.s3_name }}</span>
          </li>
        </dl>
      </ul>
      <h3 class="info-title">超参<i class="el-icon-edit-outline edit" @click="updateParameter" /></h3>
      <ul class="info-item">
        <dl class="code-dl" v-for="(e, i) in version.parameter_list" :key="i">
          <li>
            <span class="key">显示名:</span>
            <span class="val">{{ e.name }}</span>
          </li>
          <li>
            <span class="key">参数名:</span>
            <span class="val">{{ e.key }}</span>
          </li>
          <li>
            <span class="key">参数值:</span>
            <span class="val">{{ e.value }}</span>
          </li>
        </dl>
      </ul>
      <h3 class="info-title">端口<i class="el-icon-edit-outline edit" @click="updatePort" /></h3>
      <ul class="info-item">
        <dl class="code-dl" v-for="(e, i) in version.port_list" :key="i">
          <li>
            <span class="key">协议:</span>
            <span class="val">{{ e.protocol }}</span>
          </li>
          <li>
            <span class="key">端口:</span>
            <span class="val">{{ e.port }}</span>
          </li>
        </dl>
      </ul>
    </div>

    <el-dialog :visible.sync="showEnvDialog" title="开发环境基本信息" width="840px">
      <el-form class="form myself-form" ref="envForm" :rules="submitEnvRules" :model="versionNow" label-width="120px">
        <div class="node-module">
          <h3 class="node-module-title">主节点</h3>
          <el-form-item label="环境" prop="mainNodeImageId">
            <el-cascader v-model="mainNode.image_group" :options="imageOptions" :show-all-levels="false"
              class="full-extent-select"></el-cascader>
          </el-form-item>
          <el-form-item label="资源组" prop="mainProduct">
            <el-select v-model="mainNode.product_id" prop="main_product" class="full-extent-select">
              <el-option v-for="item in productOptions" :value="item.id" :label="item.name" :key="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="启动命令行" prop="mainCommand">
            <el-input type="textarea" :rows="2" v-model="mainNode.command" />
          </el-form-item>
          <!-- <el-form-item label="数量">
            <el-input-number size="mini" v-model="addForm.podNum"></el-input-number>
          </el-form-item> -->

          <div class="right-btn-box" v-if="slaveNodeList.length == 0">
            <span class="outin-handle">
              <a href="javascript:;" @click="addSlaveNode()">增加</a>
            </span>
          </div>
        </div>
        <div v-for="(tag, i) in slaveNodeList" :key="i">
          <div class="node-module">
            <h3 class="node-module-title">从节点</h3>
            <el-form-item label="环境" prop="slaveImage">
              <el-cascader v-model="tag.image_group" :options="imageOptions" :show-all-levels="false"
                class="full-extent-select"></el-cascader>
            </el-form-item>
            <el-form-item label="资源组">
              <el-select v-model="tag.product_id" class="full-extent-select">
                <el-option v-for="item in productOptions" :value="item.id" :label="item.name" :key="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="数量">
              <el-input-number v-model="tag.pod_num"></el-input-number>
            </el-form-item>
            <el-form-item label="启动命令行" prop="slaveCommand">
              <el-input type="textarea" :rows="2" v-model="tag.command" />
            </el-form-item>
            <div class="right-btn-box">
              <span class="outin-handle">
                <a href="javascript:;" @click="delSlaveNode(i)">删除</a>
                <a href="javascript:;" @click="addSlaveNode()" v-if="i == slaveNodeList.length - 1">增加</a>
              </span>
            </div>
          </div>
        </div>
        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitEnv">确定</el-button>
          <el-button class="none-type" @click="cancelEnv">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="showDataSetDialog" title="数据集" width="840px">
      <el-form class="form myself-form" ref="datasetForm" :model="versionNow">
        <el-form-item label="数据集">
          <plug-dataset :dataset_list="versionNow.dataset_list" @infoDataset="infoDataset"
            v-if="versionNow.dataset_list" />
        </el-form-item>
        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitDataset">确定</el-button>
          <el-button class="none-type" @click="cancelDataset">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="showModelDialog" title="预训练模型" width="840px">
      <el-form class="form myself-form" ref="modelForm" :model="versionNow">
        <el-form-item label="预训练模型">
          <plug-model :model_list="model_list" @infoModel="infoModel" v-if="model_list" />
        </el-form-item>
        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitModel">确定</el-button>
          <el-button class="none-type" @click="cancelModel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="showInS3Dialog" title="S3输入" width="840px">
      <el-form class="form myself-form" ref="inS3Form" :model="versionNow">

        <plug-update-s3 @onClose="closeInS3Dialog" :s3_list="in_s3_list" @infoS3="infoInS3" v-if="in_s3_list" />

        <!--        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitInS3">确定</el-button>
          <el-button class="none-type" @click="cancelInS3">取消</el-button>
        </el-form-item>-->
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="showOutS3Dialog" title="S3输出" width="840px">
      <el-form class="form myself-form" ref="outS3Form" :model="versionNow">
        <plug-update-s3 @onClose="closeOutS3Dialog" :s3_list="out_s3_list" @infoS3="infoOutS3" v-if="out_s3_list" />
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="showPortDialog" title="端口基本信息" width="840px">
      <el-form class="form myself-form" ref="portForm" :model="versionNow">
        <plug-update-port @onClose="closePortDialog" :port_list="versionNow.port_list" @infoPort="infoPort"
          v-if="versionNow.port_list" />
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="showParameterDialog" title="超参" width="840px">
      <el-form class="form myself-form" ref="portForm" :model="versionNow">

        <plug-update-parameter :parameter_list="versionNow.parameter_list" @infoParameter="infoParameter"
          @onClose="closeParamDialog" v-if="versionNow.parameter_list" />

      </el-form>
    </el-dialog>
  </div>
</template>

<script>

import plugState from '@/components/module/plug-state'
import plugDataset from '@/components/module/plug-in-dataset'
import plugModel from '@/components/module/plug-in-model'
import { getAllProducts } from '@/api/product'
import { putTaskVersion, getTaskVersionDetail } from '@/api/task'
import { getAllImageGroups, getImageDetail } from '@/api/image'
import { mainImageRule, mainProductRule, slaveImageRule } from '@/utils/rule'
import { paramMap } from '@/utils/maps'
import plugUpdatePort from '@/components/module/plug-update-port'
import plugUpdateS3 from '@/components/module/plug-update-s3'
import plugUpdateParameter from '@/components/module/plug-update-parameter'
import { getAllAssetS3 } from '@/api/inout'
export default {
  data() {
    return {
      s3_flag: paramMap['s3_flag'],
      showEnvDialog: false,
      showDataSetDialog: false,
      showModelDialog: false,
      showInS3Dialog: false,
      showOutS3Dialog: false,
      showPortDialog: false,
      showParameterDialog: false,
      imageOptions: [],
      productOptions: [],
      mainNode: { pod_num: 1, type: 0 },
      slaveNodeList: [],
      dataset_list: [],
      in_s3_list: [],
      model_list: [],
      out_s3_list: [],
      s3Options: [],
      status: null,
      ssh_list: [],
      versionNow: {},
      submitEnvRules: {
        main_image_id: [
          { required: true, message: "该项为必填项" },
        ],
        main_product: [
          { required: true, message: "该项为必填项" },
        ],
        mainNodeImageId: [
          { required: true, validator: mainImageRule.bind(this, this), trigger: 'change' }
        ],
        mainProduct: [
          { required: true, validator: mainProductRule.bind(this, this), trigger: 'change' }
        ],
        tagImageId: [
          { required: true, validator: slaveImageRule.bind(this, this), trigger: 'change' }
        ],

      },
    };
  },
  components: {
    plugDataset,
    plugUpdatePort,
    plugModel,
    plugUpdateS3,
    plugState,
    plugUpdateParameter
  },
  props: ['detailData', 'version'],
  watch: {
    async detailData(val) {
      console.log(val)
      this.getData()
    },
    version(val) {
      console.log("version", val)
      this.getData()
    }
  },

  mounted() {
    // this.status = this.version.status
    this.getS3s()
    this.getData()
    this.getAllImageGroups()
    this.getProductList()
  },
  methods: {
    lookLog(row) {
      console.log(row)
    },
    getData() {
      this.versionNow = JSON.parse(JSON.stringify(this.version))
      this.getAddForm(this.versionNow)
      this.status = this.version.status
    },
    closeOutS3Dialog() {
      this.showOutS3Dialog = false
    },
    closeInS3Dialog() {
      this.showInS3Dialog = false
    },
    closeParamDialog() {
      this.showParameterDialog = false
    },
    closePortDialog() {
      this.showPortDialog = false
    },
    formatterRole({ type }) {
      let role = {
        1: '主节点',
        2: '从节点'
      }
      return role[type]
    },
    async getAddForm(data) {
      this.slaveNodeList = []
      this.in_s3_list = []
      this.out_s3_list = []
      for (let node of data.node_group_list) {
        if (node.type == 1) {
          this.mainNode = node
          let res = await getImageDetail({ id: this.mainNode.image_id })
          let mainImage = res.data
          this.mainNode.image_group = [mainImage.visibility.toString(), mainImage.engine.toString(), mainImage.id]
        } else {
          let res = await getImageDetail({ id: node.image_id })
          let slaveImage = res.data
          let slaveNode = node
          slaveNode.image_group = [slaveImage.visibility.toString(), slaveImage.engine.toString(), slaveImage.id]
          this.slaveNodeList.push(slaveNode)
        }
      }
      if (data.s3_list.length > 0) {
        for (let s3 of data.s3_list) {
          if (s3.type == 0) {
            this.in_s3_list.push(s3)
          }
          else {
            this.out_s3_list.push(s3)
          }
        }
      }
    },
    updateEnv() {
      this.showEnvDialog = true
    },
    commitEnv() {
      this.versionNow.node_group_list = []
      this.mainNode.type = 1
      this.mainNode.image_id = this.mainNode.image_group[2]
      this.versionNow.node_group_list.push(this.mainNode)
      for (let i = 0; i < this.slaveNodeList.length; i++) {
        this.slaveNodeList[i].type = 2
        this.slaveNodeList[i].image_id = this.slaveNodeList[i].image_group[2]
        this.versionNow.node_group_list.push(this.slaveNodeList[i])
      }
      this.$refs.envForm.validate(async (valid) => {
        if (valid) {
          let res = await putTaskVersion(this.versionNow);
          if (res && res.data) {
            this.showEnvDialog = false
            this.freshVersion()
          }
        }
      })
    },
    cancelEnv() {
      this.showEnvDialog = false
    },
    addSlaveNode() {
      let data = { type: 1, pod_num: 1 }
      this.slaveNodeList.push(data)
    },
    delSlaveNode(i) {
      this.slaveNodeList.splice(i, 1)
    },
    async getAllImageGroups() {
      let res = await getAllImageGroups()
      this.imageOptions = res.data
    },
    updatePort() {
      this.showPortDialog = true
    },
    async commitPort() {
      let res = await putTaskVersion(this.versionNow);
      if (res && res.data) {
        this.showPortDialog = false
        this.freshVersion()
      }
    },
    cancelPort() {
      this.showPortDialog = false
    },
    infoPort(ports) {
      this.versionNow.port_list = ports
      this.commitPort()
    },
    updateParameter() {
      this.showParameterDialog = true
    },
    async commitParameter() {
      let res = await putTaskVersion(this.versionNow);
      if (res && res.data) {
        this.showParameterDialog = false
        this.freshVersion()
      }
    },
    cancelParameter() {
      this.showParameterDialog = false
    },
    infoParameter(parameters) {
      this.versionNow.parameter_list = parameters
      this.commitParameter()
    },
    updateDataset() {
      this.showDataSetDialog = true
    },
    async commitDataset() {
      let res = await putTaskVersion(this.versionNow);
      if (res && res.data) {
        this.showDataSetDialog = false
        this.freshVersion()
      }
    },
    async freshVersion() {
      let res = await getTaskVersionDetail({ id: this.version.id })
      this.$emit('update:version', res.data)
      // let data = res.data
      // this.version = data
    },
    cancelDataset() {
      this.showDataSetDialog = false
    },
    infoDataset(datasets) {
      datasets.forEach(e => { e.dataset_id = e.id })
      this.versionNow.dataset_list = datasets
    },

    updateModel() {
      this.showModelDialog = true
    },
    async commitModel() {
      let res = await putTaskVersion(this.versionNow);
      if (res && res.data) {
        this.showModelDialog = false
        this.freshVersion()
      }
    },
    cancelModel() {
      this.showModelDialog = false
    },
    infoModel(models) {
      this.model_list = []
      models.forEach(e => { e.model_version_id = e.id })
      this.versionNow.model_list = models
    },

    updateInS3() {
      this.showInS3Dialog = true
    },
    async getProductList() {
      let res = await getAllProducts()
      this.productOptions = res.data
    },
    async commitInS3() {
      this.versionNow.s3_list = []
      for (let i = 0; i < this.in_s3_list.length; i++) {
        this.in_s3_list[i].type = 0
        this.versionNow.s3_list.push(this.in_s3_list[i])
      }
      for (let i = 0; i < this.out_s3_list.length; i++) {
        this.out_s3_list[i].type = 1
        this.versionNow.s3_list.push(this.out_s3_list[i])
      }
      let res = await putTaskVersion(this.versionNow);
      if (res && res.data) {
        this.showInS3Dialog = false
        this.freshVersion()
      }
    },
    cancelInS3() {
      this.showInS3Dialog = false
    },
    infoInS3(s3s) {
      //console.log("in s333", s3s)
      this.in_s3_list = s3s
      this.commitInS3()
    },

    updateOutS3() {
      this.showOutS3Dialog = true
    },
    async commitOutS3() {
      this.versionNow.s3_list = []
      for (let i = 0; i < this.in_s3_list.length; i++) {
        this.in_s3_list[i].type = 0
        this.versionNow.s3_list.push(this.in_s3_list[i])
      }
      for (let i = 0; i < this.out_s3_list.length; i++) {
        this.out_s3_list[i].type = 1
        this.versionNow.s3_list.push(this.out_s3_list[i])
      }
      let res = await putTaskVersion(this.versionNow);
      if (res && res.data) {
        this.showOutS3Dialog = false
        this.freshVersion()
      }
    },
    cancelOutS3() {
      this.showOutS3Dialog = false
    },
    infoOutS3(s3s) {
      //console.log("s333", s3s)
      this.out_s3_list = s3s
      this.commitOutS3()
    },
    s3idFormat(s3_id) {
      for (let s3 of this.s3Options) {
        if (s3_id.toString() == s3.id) {
          return s3.name
        }
      }
      return s3_id
    },
    async getS3s() {
      let res = await getAllAssetS3({});
      this.s3Options = res.data
    },
  }
}
</script>
<style lang="less" scoped>
@import "~@/assets/css/code.less";
</style>
